<?php

require_once APPLICATION_PATH . '/../library/Vimeo/vimeo.php';

class VimeoFactory extends DeanFactory {
	
	private static $_instance;
	private $vimeo;
	
	public static function getInstance() {
		if(is_null(self::$_instance)){
			self::$_instance = new VimeoFactory();
		}
		return self::$_instance;
	}
	
	
	public function __construct(){
			
		// Create the object and enable caching
// 		$this->vimeo = new phpVimeo('0f2e3e031b25e3fe0dd4e622d96a8d409c59f97b', '64ac75129f127860f6382ae6546cf1b837e1783d');
// 		$this->vimeo->setToken("621060951db021fef0cdc5de0470e70e", "8d2b05a5e6c888823f4d753d64ca47469f5c9da8");
		
		$vimeo = new Vimeo(YOU_APPLICATON_ID, YOUR_APPLCATION_SECRET);
		//  Request the client credentials from the authentication server.
		$token_response = $vimeo->clientCredentials();
		
		
		$vimeo = new Vimeo(YOU_APPLICATON_ID, YOUR_APPLCATION_SECRET, ACCESS_TOKEN);
		//  Request the authenticated user's information
		$user_data = $vimeo->request('/me');
		
// // 		if (isset($_GET['clear']) && $_GET['clear'] == 'all') {
// // 			session_destroy();
// // 			session_start();
// // 		}
		
// 		// Set up variables
// 		$state = (isset($_SESSION['vimeo_state'])) ? $_SESSION['vimeo_state'] : NULL;
// 		$request_token = (isset($_SESSION['oauth_request_token'])) ? $_SESSION['oauth_request_token'] : NULL;
// 		$access_token = (isset($_SESSION['oauth_access_token'])) ? $_SESSION['oauth_access_token'] : NULL;
		
		
// 		// Coming back
// 		if ((isset($_REQUEST['oauth_token']) && $_REQUEST['oauth_token']) != NULL && (isset($_SESSION['vimeo_state']) && $_SESSION['vimeo_state'] === 'start')) {
// 			$_SESSION['vimeo_state'] = $state = 'returned';
// 		}
		
// 		// If we have an access token, set it
// 		if (isset($_SESSION['oauth_access_token']) && $_SESSION['oauth_access_token'] != null) {
// 			$vimeo->setToken($_SESSION['oauth_access_token'], $_SESSION['oauth_access_token_secret']);
// 		}
		
		
// 		// Get a new request token
// 		try {
// 			$token = $this->vimeo->getRequestToken();
// 		var_dump($token);
// 		} catch ( VimeoAPIException $e ) {
// 			echo "Encountered an API error -- code {$e->getCode()} - {$e->getMessage()}";
// 		}
		
		
// 		// Store it in the session
// 		$_SESSION['oauth_request_token'] = $token['oauth_token'];
// 		$_SESSION['oauth_request_token_secret'] = $token['oauth_token_secret'];
// 		$_SESSION['vimeo_state'] = 'start';
		
// 		// Build authorize link
// 		$authorize_link = $this->vimeo->getAuthorizeUrl($token['oauth_token'], 'write');
// 		echo "Auth link is = " . $authorize_link;
		
// 		try {
// 			$videos = $this->vimeo->call ( 'vimeo.test.null' );
// 			var_dump ( $videos );
// 		} catch ( VimeoAPIException $e ) {
// 			echo "Encountered an API error -- code {$e->getCode()} - {$e->getMessage()}";
// 		}
		
		
		
		
// 		switch ($_SESSION['vimeo_state']) {
// 			default:
		
// 				// Get a new request token
// 				$token = $vimeo->getRequestToken();
		
// 				// Store it in the session
// 				$_SESSION['oauth_request_token'] = $token['oauth_token'];
// 				$_SESSION['oauth_request_token_secret'] = $token['oauth_token_secret'];
// 				$_SESSION['vimeo_state'] = 'start';
		
// 				// Build authorize link
// 				$authorize_link = $vimeo->getAuthorizeUrl($token['oauth_token'], 'write');
		
// 				break;
		
// 			case 'returned':
		
// 				// Store it
// 				if ($_SESSION['oauth_access_token'] === NULL && $_SESSION['oauth_access_token_secret'] === NULL) {
// 					// Exchange for an access token
// 					$vimeo->setToken($_SESSION['oauth_request_token'], $_SESSION['oauth_request_token_secret']);
// 					$token = $vimeo->getAccessToken($_REQUEST['oauth_verifier']);
		
// 					// Store
// 					$_SESSION['oauth_access_token'] = $token['oauth_token'];
// 					$_SESSION['oauth_access_token_secret'] = $token['oauth_token_secret'];
// 					$_SESSION['vimeo_state'] = 'done';
		
// 					// Set the token
// 					$vimeo->setToken($_SESSION['oauth_access_token'], $_SESSION['oauth_access_token_secret']);
// 				}
		
// 				// Do an authenticated call
// 				try {
// 					$videos = $vimeo->call('vimeo.test.login');
// 					var_dump($videos);
// 				}
// 				catch (VimeoAPIException $e) {
// 					echo "Encountered an API error -- code {$e->getCode()} - {$e->getMessage()}";
// 					}
		
// 					break;
// 		}
		
// 		$videos = $this->vimeo->call('vimeo.test.login', array('user_id' => "user27033370"));
// 		var_dump($videos);
	}
	
	
}

?>